﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using DapperWrapper;
using ZLWeb.WebApiRepository.InterFace;
using DapperExtensions;
using ZLWeb.WebApiEntity.MemberCard;
using ZLWeb.WebApiEntityBase;
using Dapper;
using ZL.CRM.CommonEntity.AccOpr.Web;
using ZLWeb.WebApiEntity;
using ZLWeb.WebApiEntity.WechatCardModel;
using ZLWeb.WebApiEntityBase.Cache;
using ZLWeb.WebApiInfrastructure.Base;
using ZLWeb.WebApiRepository.Base;
using Wxmplist = ZLWeb.WebApiEntity.MemberCard.Wxmplist;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.CommonRepository;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.Core.WechatService.Domain;

namespace ZLWeb.WebApiRepository
{
    public class PaymentRepository : CRMSqlServerRespository<AccInfo>, IPaymentRepository
    {
        public PaymentRepository()
        {
        }
        public PaymentRepository(long? Accid) : base(Accid)
        {
        }

        public PaymentRepository(IDbContext context) : base(context)
        {
        }

        public PaymentRepository(IDbContext context, long? Accid) :base(context)
        {
            this.Accid = Accid.GetValueOrDefault();
        }


        public WxShowInfo GetAccWxShowInfo()
        {
            WxShowInfo accinfo = new WxShowInfo();
            DynamicParameters p= CreateDynamicParameters(
                new ParamModel(ParamLabel: "accid", Val: Accid)
                 );
            WxShowInfo item =
                ExecuteProcWithTemptable(accinfo, "pr_AccWxShowInfo_Query", "#WxShowInfo", false,
                p).FirstOrDefault();
            //if (item != null) item.Dycardno = ConfContent.EncryptToBase64Format(item.Dycardno, "trfv3_olweixinpay");
            var pitem=ReturnResponseResultInfo<WxShowInfo>(p) ?? item;
            LoadCache(pitem);
            return pitem;
        }

        private void LoadCache(WxShowInfo item)
        {
            var CacheLoader = GloabConfigCacheLoader.CreateGloabConfigCacheLoader(new WechatServiceContext());
            JltyZklbCacheItem Jitem = CacheLoader.GetJltyZklbCache();
            if (Jitem != null)
            {
                Jltycrmkyzklbt kyzklbt = Jitem.kyzklbts.FirstOrDefault(tr => tr.zklbid == item.accdisid);
                item.accdismc = kyzklbt.zklbmc;
            }
        }

        public AccEntitycard CheckBindingCardNo()
        {
            AccEntitycard reCard =new AccEntitycard();
            DynamicParameters p = CreateDynamicParameters(
                         new ParamModel(ParamLabel: "accid", Val: Accid));
            AccEntitycard item = ExecuteProcWithTemptable(reCard, "pr_AccEntitycard_query_new", "#Entitycardno", false, p).FirstOrDefault();
            return ReturnResponseResultInfo<AccEntitycard>(p) ?? item??new AccEntitycard();
        }

        public ResponseResultInfo CartAdd(ZL.CRM.CommonEntity.AccOpr.Web.AccWxCardAddDetail CardAddDetail)
        {
               DynamicParameters p = CreateDynamicParameters(new ParamModel(ParamLabel: "accid", Val: Accid));
            ExecuteProcWithTemptable(CardAddDetail.Ywcore, "pr_AccWxCardAdd_bytel_new", "#ywcore", true, p);
            return ReturnResponseResultInfo<ResponseResultInfo>(p) ?? new ResponseResultInfo();
        }

        public Wxmplist SResultHandle(Wxmplist relResult, string errcode, string errmsg)
        {
            DynamicParameters p = CreateDynamicParameters(
              new ParamModel(ParamLabel: "errcode", Val: errcode),
              new ParamModel(ParamLabel: "errmsg", Val: errmsg)
               );
            Wxmplist item = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_resulthandle", "#wxmplist", true, new { errcode, errmsg }).FirstOrDefault();
            return item;
        }

        public ZLWeb.WebApiEntity.WechatCardModel.AccInfo GetAccInfoquery()
        {
            List<Action<IDbConnection>> paActions = new List<Action<IDbConnection>>();
            ZLWeb.WebApiEntity.WechatCardModel.AccInfo accinfo = new ZLWeb.WebApiEntity.WechatCardModel.AccInfo();
            DynamicParameters p = CreateDynamicParameters(
                new ParamModel(ParamLabel: "accid", Val: Accid)
                 );
            ZLWeb.WebApiEntity.WechatCardModel.AccInfo item =
                ExecuteProcWithTemptable(accinfo, "pr_AccSta_Single_Query_new", "#AccInfo", false,
                p).FirstOrDefault();

            return ReturnResponseResultInfo<ZLWeb.WebApiEntity.WechatCardModel.AccInfo>(p) ?? item;
        }

        public RelResult GetAccWxmprel(string openappid,string code,string cardid)
        {
            RelResult relResult=new RelResult();
            DynamicParameters p = CreateDynamicParameters(
              new ParamModel(ParamLabel: "accid", Val: Accid),
              new ParamModel(ParamLabel: "openappid", Val: openappid),
              new ParamModel(ParamLabel: "code", Val: code),
              new ParamModel(ParamLabel: "cardid", Val: cardid)
               );
            RelResult item = ExecuteProcWithTemptable(relResult, "pr_AccWxmprel_query_new", "#relresult", false, p).FirstOrDefault();
            return ReturnResponseResultInfo<RelResult>(p) ?? item;
        }

        public IEnumerable<Wxmplist> GetWxmpuinfo(string appid_j)
        {
            Wxmplist relResult = new Wxmplist();
            DynamicParameters p = CreateDynamicParameters(new ParamModel(ParamLabel: "appid", Val: appid_j));
            IEnumerable<Wxmplist> items = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_query", "#wxmplist", false, p);
            return items;
        }
      

        //public Wxmplist SaveWxmpuinfo(Wxmplist relResult)
        //{
        //    DynamicParameters p = CreateDynamicParameters();
        //    Wxmplist item = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_query", "#wxmplist", true, p).FirstOrDefault();
        //    return item;
        //}

        //public Wxmplist SResultHandle(Wxmplist relResult, string errcode, string errmsg)
        //{
        //    DynamicParameters p = CreateDynamicParameters(
        //      new ParamModel(ParamLabel: "errcode", Val: errcode),
        //      new ParamModel(ParamLabel: "errmsg", Val: errmsg)
        //       );
        //    Wxmplist item = ExecuteProcWithTemptable(relResult, "pr_wxmpuinfo_resulthandle_new", "#wxmplist", true, new { errcode, errmsg }).FirstOrDefault();
        //    return item;
        //}
    }
}
